home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / NetRecord.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  10.2 KB  |  341 lines

  1. package symantec.itools.db.net;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.util.Vector;
  8. import symjava.lang.Bignum;
  9. import symjava.sql.Date;
  10. import symjava.sql.SQLException;
  11. import symjava.sql.Time;
  12. import symjava.sql.Timestamp;
  13.  
  14. public class NetRecord extends ServerObject {
  15.    int _length = 0;
  16.    byte _state = 105;
  17.    int _rowNum = 0;
  18.    Vector _fields = new Vector();
  19.    Vector _changedFields = new Vector();
  20.    InputStream _stream = null;
  21.    int _lastProj = 1;
  22.  
  23.    Vector getFields() {
  24.       return this._fields;
  25.    }
  26.  
  27.    public void setState(byte newState) {
  28.       this._state = newState;
  29.    }
  30.  
  31.    public byte getState() {
  32.       return this._state;
  33.    }
  34.  
  35.    public int getRecordNum() {
  36.       return this._rowNum;
  37.    }
  38.  
  39.    int getType() {
  40.       return 59;
  41.    }
  42.  
  43.    public synchronized void copy(NetRecord newrec) {
  44.       if (this._rowNum == newrec.getRecordNum()) {
  45.          this._state = newrec.getState();
  46.          this._fields.removeAllElements();
  47.          this._changedFields.removeAllElements();
  48.          this._fields = newrec.getFields();
  49.       }
  50.  
  51.    }
  52.  
  53.    private void checkState(boolean bforWrite) throws SQLException {
  54.       if (this._state == 105) {
  55.          throw new SQLException("Record is invalid.");
  56.       } else {
  57.          if (bforWrite) {
  58.             if (this._state == 103) {
  59.                throw new SQLException("Record has been marked for deletion.");
  60.             }
  61.  
  62.             if (this._state == 104) {
  63.                throw new SQLException("Record has been deleted.");
  64.             }
  65.          }
  66.  
  67.       }
  68.    }
  69.  
  70.    void read(DataInputStream in) throws SQLException, IOException, ErrorException {
  71.       this._length = in.readShort();
  72.       byte[] leader = new byte[4];
  73.       in.readFully(leader, 0, 4);
  74.       this._state = in.readByte();
  75.       this._rowNum = in.readInt();
  76.       this._fields = new Vector();
  77.  
  78.       while(true) {
  79.          ServerObject obj = (ServerObject)NetClass.getNextObject(in);
  80.          if (obj.getType() == 50) {
  81.             return;
  82.          }
  83.  
  84.          if (obj.getBaseType() == 57) {
  85.             this._fields.addElement(obj);
  86.          } else {
  87.             ((ServerObject)this).onObjectError(obj);
  88.          }
  89.       }
  90.    }
  91.  
  92.    void write(DataOutputStream out) throws IOException {
  93.       out.writeByte(this.getType());
  94.       out.writeShort(9);
  95.       out.writeBytes("ROW}");
  96.       out.writeByte(this._state);
  97.       out.writeInt(this._rowNum);
  98.  
  99.       for(int i = 0; i < this._changedFields.size(); ++i) {
  100.          Field f = (Field)this._changedFields.elementAt(i);
  101.          f.write(out);
  102.       }
  103.  
  104.       this._changedFields.removeAllElements();
  105.       EOT eot = new EOT();
  106.       eot.write(out);
  107.    }
  108.  
  109.    private void fieldChanged(Field f) {
  110.       if (!this._changedFields.contains(f)) {
  111.          this._changedFields.addElement(f);
  112.       }
  113.  
  114.       if (this.getState() != 102 && this.getState() != 106) {
  115.          this.setState((byte)101);
  116.       } else {
  117.          this.setState((byte)106);
  118.       }
  119.    }
  120.  
  121.    public boolean storesData(int colIndex) throws SQLException {
  122.       return this.getField(colIndex, false).storesData();
  123.    }
  124.  
  125.    public void closeStream() {
  126.       if (this._stream != null) {
  127.          try {
  128.             this._stream.close();
  129.          } catch (Exception var1) {
  130.          }
  131.  
  132.          this._stream = null;
  133.       }
  134.  
  135.    }
  136.  
  137.    private Field getField(int proj, boolean bforWrite) throws SQLException {
  138.       this.checkState(bforWrite);
  139.       this.closeStream();
  140.       this._lastProj = proj;
  141.  
  142.       try {
  143.          return (Field)this._fields.elementAt(proj - 1);
  144.       } catch (ArrayIndexOutOfBoundsException var3) {
  145.          throw new SQLException("Column index is out-of-range: " + proj);
  146.       } catch (Exception var4) {
  147.          throw new SQLException("Record not initialized.");
  148.       }
  149.    }
  150.  
  151.    public synchronized boolean isNull(int proj) throws SQLException {
  152.       return this.getField(proj, false).isNull();
  153.    }
  154.  
  155.    public synchronized boolean wasNull() throws SQLException {
  156.       return this.isNull(this._lastProj);
  157.    }
  158.  
  159.    public synchronized String getString(int proj) throws SQLException {
  160.       return this.getField(proj, false).getString();
  161.    }
  162.  
  163.    public synchronized boolean getBoolean(int proj) throws SQLException {
  164.       return this.getField(proj, false).getBoolean();
  165.    }
  166.  
  167.    public synchronized byte getByte(int proj) throws SQLException {
  168.       return this.getField(proj, false).getByte();
  169.    }
  170.  
  171.    public synchronized short getShort(int proj) throws SQLException {
  172.       return this.getField(proj, false).getShort();
  173.    }
  174.  
  175.    public synchronized int getInt(int proj) throws SQLException {
  176.       return this.getField(proj, false).getInt();
  177.    }
  178.  
  179.    public synchronized long getLong(int proj) throws SQLException {
  180.       return this.getField(proj, false).getLong();
  181.    }
  182.  
  183.    public synchronized float getFloat(int proj) throws SQLException {
  184.       return this.getField(proj, false).getFloat();
  185.    }
  186.  
  187.    public synchronized double getDouble(int proj) throws SQLException {
  188.       return this.getField(proj, false).getDouble();
  189.    }
  190.  
  191.    public synchronized Bignum getBignum(int proj, int scale) throws SQLException {
  192.       return this.getField(proj, false).getBignum(scale);
  193.    }
  194.  
  195.    public synchronized byte[] getBytes(int proj) throws SQLException {
  196.       return this.getField(proj, false).getBytes();
  197.    }
  198.  
  199.    public synchronized Date getDate(int proj) throws SQLException {
  200.       return this.getField(proj, false).getDate();
  201.    }
  202.  
  203.    public synchronized Time getTime(int proj) throws SQLException {
  204.       return this.getField(proj, false).getTime();
  205.    }
  206.  
  207.    public synchronized Timestamp getTimestamp(int proj) throws SQLException {
  208.       return this.getField(proj, false).getTimestamp();
  209.    }
  210.  
  211.    public synchronized InputStream getAsciiStream(int proj) throws SQLException {
  212.       return this.getField(proj, false).getAsciiStream();
  213.    }
  214.  
  215.    public synchronized InputStream getUnicodeStream(int proj) throws SQLException {
  216.       return this.getField(proj, false).getUnicodeStream();
  217.    }
  218.  
  219.    public synchronized InputStream getBinaryStream(int proj) throws SQLException {
  220.       return this.getField(proj, false).getBinaryStream();
  221.    }
  222.  
  223.    public synchronized void setNull(int proj, int sqlType) throws SQLException {
  224.       Field f = this.getField(proj, true);
  225.       f.setNull(sqlType);
  226.       this.fieldChanged(f);
  227.    }
  228.  
  229.    public synchronized void setBoolean(int proj, boolean x) throws SQLException {
  230.       Field f = this.getField(proj, true);
  231.       f.setBoolean(x);
  232.       this.fieldChanged(f);
  233.    }
  234.  
  235.    public synchronized void setByte(int proj, byte x) throws SQLException {
  236.       Field f = this.getField(proj, true);
  237.       f.setByte(x);
  238.       this.fieldChanged(f);
  239.    }
  240.  
  241.    public synchronized void setShort(int proj, short x) throws SQLException {
  242.       Field f = this.getField(proj, true);
  243.       f.setShort(x);
  244.       this.fieldChanged(f);
  245.    }
  246.  
  247.    public synchronized void setInt(int proj, int x) throws SQLException {
  248.       Field f = this.getField(proj, true);
  249.       f.setInt(x);
  250.       this.fieldChanged(f);
  251.    }
  252.  
  253.    public synchronized void setLong(int proj, long x) throws SQLException {
  254.       Field f = this.getField(proj, true);
  255.       f.setLong(x);
  256.       this.fieldChanged(f);
  257.    }
  258.  
  259.    public synchronized void setFloat(int proj, float x) throws SQLException {
  260.       Field f = this.getField(proj, true);
  261.       f.setFloat(x);
  262.       this.fieldChanged(f);
  263.    }
  264.  
  265.    public synchronized void setDouble(int proj, double x) throws SQLException {
  266.       Field f = this.getField(proj, true);
  267.       f.setDouble(x);
  268.       this.fieldChanged(f);
  269.    }
  270.  
  271.    public synchronized void setBignum(int proj, Bignum x) throws SQLException {
  272.       Field f = this.getField(proj, true);
  273.       f.setBignum(x);
  274.       this.fieldChanged(f);
  275.    }
  276.  
  277.    public synchronized void setString(int proj, String x) throws SQLException {
  278.       Field f = this.getField(proj, true);
  279.       f.setString(x);
  280.       this.fieldChanged(f);
  281.    }
  282.  
  283.    public synchronized void setBytes(int proj, byte[] x) throws SQLException {
  284.       Field f = this.getField(proj, true);
  285.       f.setBytes(x);
  286.       this.fieldChanged(f);
  287.    }
  288.  
  289.    public synchronized void setDate(int proj, Date x) throws SQLException {
  290.       Field f = this.getField(proj, true);
  291.       f.setDate(x);
  292.       this.fieldChanged(f);
  293.    }
  294.  
  295.    public synchronized void setTime(int proj, Time x) throws SQLException {
  296.       Field f = this.getField(proj, true);
  297.       f.setTime(x);
  298.       this.fieldChanged(f);
  299.    }
  300.  
  301.    public synchronized void setTimestamp(int proj, Timestamp x) throws SQLException {
  302.       Field f = this.getField(proj, true);
  303.       f.setTimestamp(x);
  304.       this.fieldChanged(f);
  305.    }
  306.  
  307.    public synchronized void setAsciiStream(int proj, InputStream x, int length) throws SQLException {
  308.       Field f = this.getField(proj, true);
  309.       f.setAsciiStream(x, length);
  310.       this.fieldChanged(f);
  311.    }
  312.  
  313.    public synchronized void setUnicodeStream(int proj, InputStream x, int length) throws SQLException {
  314.       Field f = this.getField(proj, true);
  315.       f.setUnicodeStream(x, length);
  316.       this.fieldChanged(f);
  317.    }
  318.  
  319.    public synchronized void setBinaryStream(int proj, InputStream x, int length) throws SQLException {
  320.       Field f = this.getField(proj, true);
  321.       f.setBinaryStream(x, length);
  322.       this.fieldChanged(f);
  323.    }
  324.  
  325.    public synchronized int getSQLType(int proj) throws SQLException {
  326.       Field f = this.getField(proj, false);
  327.       return f.getSQLType();
  328.    }
  329.  
  330.    public synchronized Object getObject(int proj) throws SQLException {
  331.       Field f = this.getField(proj, false);
  332.       return f.getObject();
  333.    }
  334.  
  335.    synchronized void setObject(int proj, Object obj) throws SQLException {
  336.       Field f = this.getField(proj, true);
  337.       f.setObject(obj);
  338.       this.fieldChanged(f);
  339.    }
  340. }
  341.